package com.seventh.restaurant.service.Impl;

import com.seventh.restaurant.domain.BookingRecord;
import com.seventh.restaurant.domain.Customer;
import com.seventh.restaurant.domain.Table;
import com.seventh.restaurant.repository.BookingRecordRepository;
import com.seventh.restaurant.repository.CustomerRepository;
import com.seventh.restaurant.repository.TableRepository;
import com.seventh.restaurant.service.BookingRecordService;
import com.seventh.restaurant.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookingRecordServiceImpl implements BookingRecordService {

    private  final BookingRecordRepository bookingRecordRepository;
    private final CustomerRepository customerRepository;
    private final TableRepository tableRepository;

    public BookingRecordServiceImpl(BookingRecordRepository bookingRecordRepository,
                                    CustomerRepository customerRepository,
                                    TableRepository tableRepository) {
        this.bookingRecordRepository = bookingRecordRepository;
        this.customerRepository = customerRepository;
        this.tableRepository = tableRepository;
    }

    @Override
    public BookingRecord getOneBookingRecord(Long id) throws Exception {
        return  bookingRecordRepository.getOneBooking(id);
    }

    @Override
    public BookingRecord insertBookingRecord(BookingRecord bookingRecord) throws Exception {
        String phonenumber = bookingRecord.getCustomer().getPhoneNumber();
        Customer customer = customerRepository.findByPhoneNumber(phonenumber);
        if (customer==null){
            Customer customer1 = new Customer();
            customer1.setName(bookingRecord.getCustomer().getName());
            customer1.setPhoneNumber(bookingRecord.getCustomer().getPhoneNumber());
            customerRepository.save(customer1);
        }else {
            customer.setName(bookingRecord.getCustomer().getName());
            customerRepository.save(customer);
        }
        Customer customer2 = customerRepository.findByPhoneNumber(phonenumber);
        bookingRecord.setCustomer(customer2);
        bookingRecord.setRecordCancle("0");
        bookingRecord.setIsArrival("0");
        bookingRecord.setIsOver("0");
        Table table = tableRepository.findByTableNo(bookingRecord.getTableNo());
        table.setIsRecord("1");
        tableRepository.save(table);
        bookingRecord = bookingRecordRepository.save(bookingRecord);
        if (bookingRecord.getId()>0){
            return bookingRecord;
        }else {
            throw new Exception("预定失败");
        }
    }

    @Override
    public BookingRecord cancleBookingRecord(Long id) throws Exception {
        BookingRecord bookingRecord = bookingRecordRepository.getOne(id);
        if (bookingRecord==null){
            throw new Exception("系统错误，未查到该预约信息，预约ID为"+id);
        }else {
            String status = bookingRecord.getRecordCancle();
            if (status.equals("1")){
                throw new Exception("该预约已经取消，不可重复取消，预约ID为"+id);
            }else {
                String tableno = bookingRecord.getTableNo();
                Table table = tableRepository.findByTableNo(tableno);
                table.setIsBusy("0");
                table.setIsRecord("0");
                tableRepository.save(table);
                bookingRecord.setRecordCancle("1");
                bookingRecord = bookingRecordRepository.save(bookingRecord);
                if (bookingRecord.getId()<=0){
                    throw new Exception("取消预约失败，预约ID为"+id);
                }else {
                    return  bookingRecord;
                }
            }
        }

    }

    @Override
    public BookingRecord updateBookingRecord(Long id,String peoplenum,String tableno,String arrivaltime, String isarrival,String isover,String recordcancle) throws Exception {
       BookingRecord bookingRecord1 = bookingRecordRepository.getOne(id);
       if (bookingRecord1==null){
           throw new Exception("错误，未查到该预约信息，预约ID为"+id);
       }else {
           String oldtable = bookingRecord1.getTableNo();
           if (oldtable!=tableno){
               Table table = tableRepository.findByTableNo(oldtable);
               table.setIsRecord("0");
               table.setIsBusy("0");
               tableRepository.save(table);
               Table table1 = tableRepository.findByTableNo(tableno);
               table1.setIsRecord("1");
               if (isarrival.equals("1")){
                   table1.setIsBusy("1");
               }else {
                   table1.setIsBusy("0");
               }
               if (isover.equals("1") ||recordcancle.equals("1")){
                   table1.setIsRecord("0");
                   table1.setIsBusy("0");
               }
               tableRepository.save(table1);
               bookingRecord1.setPeopleNum(peoplenum);
               bookingRecord1.setTableNo(tableno);
               bookingRecord1.setArrivalTime(arrivaltime);
               bookingRecord1.setIsArrival(isarrival);
               bookingRecord1.setIsOver(isover);
               bookingRecord1.setRecordCancle(recordcancle);
               bookingRecord1 = bookingRecordRepository.save(bookingRecord1);
               if (bookingRecord1.getId()<=0){
                   throw new Exception("修改预约信息错误，预约ID为"+bookingRecord1.getId());
               }else {
                   return bookingRecord1;
               }
           }else {
               Table table1 = tableRepository.findByTableNo(tableno);
               table1.setIsRecord("1");
               if (isarrival.equals("1")){
                   table1.setIsBusy("1");
               }else {
                   table1.setIsBusy("0");
               }
               if (isover.equals("1") ||recordcancle.equals("1")){
                   table1.setIsRecord("0");
                   table1.setIsBusy("0");
               }
               tableRepository.save(table1);
               bookingRecord1.setPeopleNum(peoplenum);
               bookingRecord1.setTableNo(tableno);
               bookingRecord1.setArrivalTime(arrivaltime);
               bookingRecord1.setIsArrival(isarrival);
               bookingRecord1.setIsOver(isover);
               bookingRecord1.setRecordCancle(recordcancle);
               bookingRecord1 = bookingRecordRepository.save(bookingRecord1);
               if (bookingRecord1.getId()<=0){
                   throw new Exception("修改预约信息错误，预约ID为"+bookingRecord1.getId());
               }else {
                   return bookingRecord1;
               }
           }
       }
    }

    @Override
    public List<BookingRecord> findBookingRecord(String phonenumber) {
        return bookingRecordRepository.findByCustomerPhoneNumberOrderByBookingTimeDesc(phonenumber);
    }

    @Override
    public List<BookingRecord> findALLBookingRecords(String kind) {
        if (kind.equals("2")){
            return bookingRecordRepository.findAll();
        }else if(kind.equals("3")){
            return  bookingRecordRepository.findByIsOver("1");
        }else {
            return bookingRecordRepository.findByRecordCancle(kind);

        }

    }

    @Override
    public BookingRecord endBookingRecord(Long id) throws Exception {
        BookingRecord bookingRecord = bookingRecordRepository.getOne(id);
        if (bookingRecord==null){
            throw new Exception("系统错误，未查到该预约信息，预约ID为"+id);
        }else {
            String status = bookingRecord.getIsOver();
            if (status.equals("1")){
                throw new Exception("用餐已经结束，不可重复结束用餐，预约ID为"+id);
            }else {
                String tableno = bookingRecord.getTableNo();
                Table table = tableRepository.findByTableNo(tableno);
                table.setIsBusy("0");
                table.setIsRecord("0");
                tableRepository.save(table);
                bookingRecord.setIsOver("1");
                bookingRecord.setIsArrival("0");
                bookingRecord.setRecordCancle("0");
                bookingRecord = bookingRecordRepository.save(bookingRecord);
                if (bookingRecord.getId()<=0){
                    throw new Exception("结束用餐失败，预约ID为"+id);
                }else {
                    return  bookingRecord;
                }
            }
        }
    }

    @Override
    public BookingRecord startBookingRecord(Long id) throws Exception {
        BookingRecord bookingRecord = bookingRecordRepository.getOne(id);
        if (bookingRecord==null){
            throw new Exception("系统错误，未查到该预约信息，预约ID为"+id);
        }else {
            String status = bookingRecord.getIsArrival();
            if (status.equals("1")){
                throw new Exception("用餐已经开始，不可重复开始用餐，预约ID为"+id);
            }else {
                String tableno = bookingRecord.getTableNo();
                Table table = tableRepository.findByTableNo(tableno);
                table.setIsBusy("1");
                table.setIsRecord("1");
                tableRepository.save(table);
                bookingRecord.setIsOver("0");
                bookingRecord.setIsArrival("1");
                bookingRecord.setRecordCancle("0");
                bookingRecord = bookingRecordRepository.save(bookingRecord);
                if (bookingRecord.getId()<=0){
                    throw new Exception("开始用餐失败，预约ID为"+id);
                }else {
                    return  bookingRecord;
                }
            }
        }
    }
}
